package io.gitee.kotle.demo.mvvm_demo.pic


import android.view.View
import coil.Coil
import coil.request.SuccessResult
import coil.result
import coil.transform.CircleCropTransformation
import com.bumptech.glide.Glide
import io.gitee.kotle.android.BaseGlobalConst
import io.gitee.kotle.android.adapter.RecyclerViewAdapter
import io.gitee.kotle.android.adapter.RecyclerViewHolder
import io.gitee.kotle.android.adapter.requireData
import io.gitee.kotle.android.utils.*
import io.gitee.kotle.android.utils.glide.setImageByGlide
import io.gitee.kotle.demo.databinding.HolderPicBinding


class PicHolder : RecyclerViewHolder<PicBean.ImagesDTO, HolderPicBinding> {
    constructor(binding: HolderPicBinding) : super(binding)
    constructor(itemView: View) : super(itemView)

    init {
        itemView.onClick {
            val result = mBinding.root.result.asOrNull<SuccessResult>()
            val memoryCacheKey = result?.memoryCacheKey
            val memoryCacheKeyStr = memoryCacheKey?.key
            val diskCacheKey = result?.diskCacheKey
            println("===========${bindingPosition},${result?.dataSource}")
            mBinding.root.result?.request?.memoryCacheKey
            PicFragment.startBlur(
                it.context,
                bindingAdapter.asOrNull<RecyclerViewAdapter<Any>>()!!.currentList.filter {
                    it is PicBean.ImagesDTO
                } as List<PicBean.ImagesDTO>
            )
        }
    }


    override fun bindData(data: PicBean.ImagesDTO) {
        if (data.isBlur) {
            mBinding.root.setImageByCoil(data.url) {
                it.transformations(BlurTransformation(20), CircleCropTransformation())
                it.placeholderMemoryCacheKey(data.url)
            }
        } else {
            mBinding.root.setImageByCoil(data.url)
        }

    }

    override fun onViewDetachedFromWindow() {
        super.onViewDetachedFromWindow()
        mBinding.root.clearImageByCoil()
    }
}